package cc.fs.zhihuapp.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.Theme;

/**
 * Created by fostion on 2015/9/7.
 */
public class ThemeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //        var onItemClickListener: ((Theme) -> Unit)? = null
    private Context context;
    private ArrayList<Theme> list;
    private ThemeAdapter.OnItemClick onItemClick;

    public ThemeAdapter(Context context,ArrayList<Theme> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_theme, parent, false);
        return new ItemViewHodler(v);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHodler) {
            ItemViewHodler itemViewHodler = (ItemViewHodler) holder;
            itemViewHodler.setData(list.get(position));
        }
    }

    private class ItemViewHodler extends RecyclerView.ViewHolder {
        TextView title;

        public ItemViewHodler(View itemView) {
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.title);
        }

        void setData(final Theme theme) {
            title.setText(theme.getName());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClick != null){
                        onItemClick.click(theme);
                    }
                }
            });
        }
    }

    public void setOnItemClick(ThemeAdapter.OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick{
        void click(Theme theme);
    }


}
